package com.sale.system.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sale.common.core.constant.CacheNames;
import com.sale.common.core.utils.StringUtils;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import com.sale.common.redis.utils.CacheUtils;
import com.sale.common.redis.utils.RedisUtils;
import com.sale.common.satoken.utils.LoginHelper;
import com.sale.system.api.domain.SysDictData;
import com.sale.system.domain.SysTheAlarm;
import com.sale.system.domain.SysWorkbenchMessage;
import com.sale.system.domain.dto.SysWorkbenchMessageDto;
import com.sale.system.domain.vo.SysWorkbenchMessageVo;
import com.sale.system.mapper.SysTheAlarmMapper;
import com.sale.system.mapper.SysUserRoleMapper;
import com.sale.system.mapper.SysWorkbenchMessageMapper;
import com.sale.system.service.SysWorkbenchMessageService;
import lombok.RequiredArgsConstructor;
import net.sf.jsqlparser.expression.StringValue;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 消息提醒实现层
 */
@RequiredArgsConstructor
@Service
public class SysWorkbenchMessageServiceImpl implements SysWorkbenchMessageService {

    /**
     * 消息提醒mapper
     */
    private final SysWorkbenchMessageMapper sysWorkbenchMessageMapper;

    private final SysTheAlarmMapper sysTheAlarmMapper;

    private final SysUserRoleMapper sysUserRoleMapper;

    /**
     * 消息提醒列表
     *
     * @param sysWorkbenchMessageVo
     * @return
     */
    @Override
    public TableDataInfo<SysWorkbenchMessageDto> getList(SysWorkbenchMessageVo sysWorkbenchMessageVo, PageQuery pageQuery) {
        //获取消息提醒类型
        List<SysDictData> statusArray = CacheUtils.get(CacheNames.SYS_DICT, "message_type");
        Map<String, String> messageMap = statusArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        Page<SysWorkbenchMessageDto> messageList = sysWorkbenchMessageMapper.getMessageList(sysWorkbenchMessageVo,pageQuery.build());
        List<SysWorkbenchMessageDto> records = messageList.getRecords();
        for (SysWorkbenchMessageDto entity : records) {
            //类型
            entity.setMessageTypeDict(messageMap.get(entity.getMessageType()));
        }
        return TableDataInfo.build(messageList);
    }

    /**
     * 修改
     *
     * @param sysWorkbenchMessageVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateStatus(SysWorkbenchMessageVo sysWorkbenchMessageVo) {
        if (sysWorkbenchMessageVo.getId() != null) {
            //根据id修改单个数据
            return sysWorkbenchMessageMapper.updateStatus(sysWorkbenchMessageVo.getId()) > 0;
        } else {
            //获取全部列表
            PageQuery pageQuery = new PageQuery();
            Page<SysWorkbenchMessageDto> messageList = sysWorkbenchMessageMapper.getMessageList(sysWorkbenchMessageVo,pageQuery.build());
            List<SysWorkbenchMessageDto> records = messageList.getRecords();
            List<String> idList = new ArrayList<>();
            for (SysWorkbenchMessageDto entity : records) {
                idList.add(String.valueOf(entity.getId()));
            }
            //修改全部
            return sysWorkbenchMessageMapper.updateStatusByIds(idList) > 0;
        }
    }

    /**
     * 新增消息提醒
     *
     * @param list
     * @return
     */
    @Override
    public boolean insertWork(List<SysWorkbenchMessageVo> list) {
        List<SysWorkbenchMessage> resultList = new ArrayList<>();
        for(SysWorkbenchMessageVo entity : list){
            SysWorkbenchMessage sysWorkbenchMessage = new SysWorkbenchMessage();
            BeanUtils.copyProperties(entity,sysWorkbenchMessage);
            resultList.add(sysWorkbenchMessage);
        }
        return sysWorkbenchMessageMapper.insertBatch(resultList);
    }

    /**
     * 查询对应消息类别的列表
     *
     * @param type
     * @return
     */
    @Override
    public List<SysWorkbenchMessage> getInfo(String type) {
        return sysWorkbenchMessageMapper.getInfo(type);
    }

    /**
     * 查询未读消息数量
     *
     * @param
     * @return
     */
    @Override
    public int getNoMessage() {
        SysWorkbenchMessageVo sysWorkbenchMessageVo = new SysWorkbenchMessageVo();
        Long userId = LoginHelper.getUserId();
        sysWorkbenchMessageVo.setReadMark(2);
        sysWorkbenchMessageVo.setUserId(userId);
        PageQuery pageQuery = new PageQuery();
        return sysWorkbenchMessageMapper.getMessageList(sysWorkbenchMessageVo, pageQuery.build()).getRecords().size();
    }

    /**
     * 获取告警信息
     * @param materialCode
     * @return
     */
    @Override
    public SysTheAlarm getAlarm(String materialCode, String type) {
        SysTheAlarm alarm = sysTheAlarmMapper.getAlarm(materialCode, type);

        if (alarm!=null){

            String alarmRoleId = alarm.getAlarmRoleId();
            String[] roles = alarmRoleId.split(",");



            //通过角色id查出用户列表
            List<Long> sysUserRole = sysUserRoleMapper.getUserList(roles);

            String role = "";

            for (int i = 0; i < sysUserRole.size(); i++) {

                role=role+sysUserRole.get(i);

                if (i!=sysUserRole.size()-1){
                    role= role+",";
                }
            }
            //使用用户id覆盖角色id
            alarm.setAlarmRoleId(role);}

        return alarm;
    }


}
